package com.wry.utils;

import com.wry.model.Order;
import lombok.Data;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <h1>  <h1>
 *
 * @author wry
 * @since 2023/8/21 11:18
 */
@Data
public class PageResponse<T> implements Serializable {

    private T data;

    private Long total;

    private List<T> result;

    public static <T> PageResponse<T> page(SearchHits<T> searchHits){
        final List<T> entityList = searchHits.get().map(SearchHit::getContent).collect(Collectors.toList());
        PageResponse<T> response = new PageResponse<>();
        response.setResult(entityList);
        response.setTotal(searchHits.getTotalHits());
        return response;
    }
}
